Skip to content

refactor: split logging module + cleanup + lifecycle test#99

Merged
lesnik512 merged 1 commit into
mainfrom
refactor/ref-4-logging-cleanup
Jun 1, 2026
Merged

refactor: split logging module + cleanup + lifecycle test#99
lesnik512 merged 1 commit into
mainfrom
refactor/ref-4-logging-cleanup

Conversation

@lesnik512

Copy link
Copy Markdown
Member

Summary

The biggest PR of the deferred-refactors sequence — six logging-area items bundled:

  • REF-4: Split logging_instrument.py (212 lines, four concerns) into a new logging_factory.py (MemoryLoggerFactory + serializer + protocols + the new internal _MemoryLoggerFactoryConfig) and a slimmer logging_instrument.py (config + instrument + tracer injection). _MemoryLoggerFactoryConfig and _serialize_log_with_orjson_to_string live OUTSIDE the structlog gate so logging_instrument.py can import them unconditionally; MemoryLoggerFactory (which inherits from structlog.stdlib.LoggerFactory) stays inside. Backward compatibility: the moved public symbols are re-imported into logging_instrument.py with explicit __all__, so existing from lite_bootstrap.instruments.logging_instrument import MemoryLoggerFactory continues to work.
  • REF-2: Deleted the dead if import_checker.is_structlog_installed and import_checker.is_litestar_installed: check in LitestarLoggingInstrument.bootstrap(). The instrument couldn't have been registered without both packages installed.
  • LOW-6: MemoryLoggerFactory.__init__ now takes a single config: _MemoryLoggerFactoryConfig kwarg instead of four logging-config kwargs. Underscore-prefixed (internal); two existing direct-construction tests updated to use the new constructor.
  • LOW-8: Added a docstring to LoggingInstrument._unset_handlers documenting that the mutation is permanent — teardown() does NOT restore.
  • LOW-9: Added a docstring to LoggingInstrument.teardown() documenting that root logger level is unconditionally reset to WARNING.
  • TEST-8: New test_logging_instrument_lifecycle_replay exercises bootstrap → teardown → bootstrap → teardown and verifies the instrument is functional after the second bootstrap.

No external behavior change. 128/128 tests pass; ty check and ruff both clean.

Closes REF-2, REF-4, LOW-6, LOW-8, LOW-9, TEST-8 from an internal audit.

Test plan

  • just test -- tests/instruments/test_logging_instrument.py -v — passes (updated factory tests + new lifecycle replay).
  • just test — 128/128.
  • just lint — clean (no # noqa: PLR2004).
  • Reviewer: confirm the backward-compat re-imports in logging_instrument.py cover all the public symbols (MemoryLoggerFactory, AddressProtocol, RequestProtocol, ScopeType).
  • Reviewer: confirm the conditional gate restructure in logging_factory.py (_MemoryLoggerFactoryConfig and serializer outside the gate; only MemoryLoggerFactory inside) is the right shape.

Notes for reviewer

The _MemoryLoggerFactoryConfig is intentionally underscore-prefixed — it's an internal helper, not new public API. Tests that construct MemoryLoggerFactory directly need to import it explicitly. The trade-off was discussed in the plan: extending the public API surface with a non-prefixed name vs accepting that tests reach into the internal config. The latter is consistent with how tests already access other internal-only types.

🤖 Generated with Claude Code

REF-4: Split lite_bootstrap/instruments/logging_instrument.py (212
lines, four concerns) into:
- logging_factory.py: MemoryLoggerFactory, _MemoryLoggerFactoryConfig,
  _serialize_log_with_orjson_to_string, AddressProtocol,
  RequestProtocol, ScopeType. Single structlog-conditional gate at
  module top.
- logging_instrument.py: LoggingConfig, LoggingInstrument,
  tracer_injection. Re-imports the moved public symbols
  (MemoryLoggerFactory, AddressProtocol, RequestProtocol, ScopeType)
  for backward compatibility — existing imports from
  lite_bootstrap.instruments.logging_instrument continue to work.

LOW-6: MemoryLoggerFactory.__init__ now takes a single
_MemoryLoggerFactoryConfig dataclass instead of four logging-config
kwargs. The dataclass is underscore-prefixed (internal); tests import
it explicitly when constructing the factory directly.

REF-2: Delete the dead `if import_checker.is_structlog_installed and
import_checker.is_litestar_installed:` defensive check in
LitestarLoggingInstrument.bootstrap. The check is unreachable —
the instrument couldn't have been registered without both packages
installed.

LOW-8: Add a docstring to LoggingInstrument._unset_handlers documenting
that the mutation is permanent (teardown does not restore handlers).

LOW-9: Add a docstring to LoggingInstrument.teardown documenting that
root logger level is unconditionally reset to WARNING.

TEST-8: Add test_logging_instrument_lifecycle_replay exercising
bootstrap → teardown → bootstrap → teardown and verifying the
instrument is functional after the second bootstrap.

No external behavior change. Updated factory tests to use the new
config-based constructor.

Closes REF-2, REF-4, LOW-6, LOW-8, LOW-9, TEST-8 from the audit.
@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...e_bootstrap/bootstrappers/litestar_bootstrapper.py 100.00% <100.00%> (ø)
lite_bootstrap/instruments/logging_factory.py 100.00% <100.00%> (ø)
lite_bootstrap/instruments/logging_instrument.py 100.00% <100.00%> (ø)
tests/instruments/test_logging_instrument.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lesnik512 lesnik512 self-assigned this Jun 1, 2026
@lesnik512 lesnik512 merged commit 24820b4 into main Jun 1, 2026
8 checks passed
@lesnik512 lesnik512 deleted the refactor/ref-4-logging-cleanup branch June 1, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant